#define _CRT_SECURE_NO_WARNINGS 1
#define _DEFAULT_SOURCE
#include"utils.h"
bool splicing(char* result_path)
{
	return opendir(result_path) == NULL ? false : true;
}
/// <summary>
/// 获取用户字符串后分割有效字符串拼接成链表
/// </summary>
/// <param name="list">用户态链表</param>
/// <param name="path">传入用户命令地址(注:不可传入const字符串,函数体内使用了strtok)</param>
/// <returns>命令地址是否拼接成功或失败</returns>
bool address_processing_function(link_list* list, char* path,char*root)
{
	if (path[0] == '/')//当写入绝对地址
	{
		clear_linklist(list);
		//char* initial = (char*)calloc(16, sizeof(char));
		//initial[0] = '/';
		//insert_linknode(list, list->size, initial);
		//insert_linknode(list, list->size, "/home/tony/code_book/thread_pool_book_test/server_thread");
		insert_linknode(list, list->size, root);
		path = path + 1;
		//ERROR_CHECK(initial, NULL, "calloc");
		char* address = strtok(path, "/");
		while (address != NULL)
		{
			if ((!strcmp(address, ".")) || (!strcmp(address, "..")))
				return false;
			insert_linknode(list, list->size, address);
			address = strtok(NULL, "/");
		}
	}
	else
	{
		char* node_address = strtok(path, "/");
		char* path = (char*)calloc(16, sizeof(char));
		while (node_address != NULL)
		{
			if (!strcmp(node_address, "."))//当前目录
			{
				node_address = strtok(NULL, "/");
				continue;
			}
				
			else if (!strcmp(node_address, ".."))//上级目录删掉最后节点
			{
				bool status = delete_linknode(list, list->size - 1, path);
				if (status == false)
					return false;
			}

			else
			{
				insert_linknode(list, list->size, node_address);
			}
			node_address = strtok(NULL, "/");
		}
	}
	//printf_linklist(list);
	return true;
}
/// <summary>
/// 初始化链表并打印用户态根目录地址
/// </summary>
/// <param name="list">已定义申请空间的list</param>
/// <param name="path">传出用户态地址</param>
void Frist_show_path(link_list* list,char *path)
{
	//list = init_linklist();
	insert_linknode(list, 0, "/");
	strcpy(path, list->head->value);
}
/// <summary>
/// 获取管态地址后将链表中地址拼接成管态地址
/// </summary>
/// <param name="list">用户态地址链表</param>
/// <param name="path">传入管态地址</param>
/// <param name="result_path">传出管态地址,此处是在函数体内malloc注意用完free</param>
/// <returns>操作是否成功</returns>
bool splicing_pipe_state_address(link_list* list, const char* path, char* result_path)
{
	if (empty_linklist(list))
		return false;
	link_node* node = NULL;
	int flag = 0;
	if (!strcmp(list->head->value, "/"))
	{
		node = list->head->next;
		flag = 1;
	}
	else
	{
		node= list->head;
		flag = 0;
	}
	while (node != NULL)
	{
		if (flag == 0)
		{
			sprintf(result_path, "%s%s/", result_path, node->value);
		}
		else
		{
			sprintf(result_path, "%s/%s", result_path, node->value);
		}
		node = node->next;
	}
	if (result_path[strlen(result_path) - 1] == '/')
		result_path[strlen(result_path) - 1] = 0;
	//printf("%s\n", result_path);
	return true;
}
/// <summary>
/// 获取地址下所有文件信息
/// </summary>
/// <param name="path">管态物理地址</param>
/// <param name="message">传入文件信息</param>
/// <returns>是否成功</returns>
int readdir_name(char* path, char* message)
{
	DIR* dp = opendir(path);
	ERROR_CHECK(dp, NULL, "opendir");
	struct dirent* dir_info;
	while ((dir_info = readdir(dp)) != NULL)
	{
		if ((!strcmp(dir_info->d_name, ".")) || (!strcmp(dir_info->d_name, "..")))
			continue;
		else if (dir_info->d_type == DT_DIR)
		{
			sprintf(message, "%s%s/\n", message, dir_info->d_name);
		}
		else
		{
			sprintf(message, "%s%s\n", message, dir_info->d_name);
		}
	}
	closedir(dp);
	return 1;
}
/// <summary>
/// 
/// </summary>
/// <param name="path"></param>
/// <param name="flag"></param>
/// <returns></returns>
bool remove_file(char* path, int flag)
{
	return false;
}
// int main()
// {
// 	link_list* user_list = init_linklist();
// 	insert_linknode(user_list, 0, "/home/tony/code_book/thread_pool_book_test/server_thread");
// 	while (1)
// 	{
// 		char str[4096] = {0};
// 		scanf("%s", str);
// 		//char str[] = "/home/tony/code_book/thread_pool_book_test/server_thread";
// 		address_processing_function(user_list, str);
// 		printf("list\n");
// 		printf_linklist(user_list);
// 		char temp[4096] = { 0 };
// 		splicing_pipe_state_address(user_list, "", temp);
// 		printf("temp=%s\n", temp);
// 	}
// }